package com.recharge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.recharge.common.domain.PageDO;
import com.recharge.common.enums.ErrorCode;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.ResultSupport;
import com.recharge.common.util.http.MapTool;
import com.recharge.core.entity.MerchantTableEntity;
import com.recharge.core.mapper.MerchantTableMapper;
import com.recharge.core.mapper.RoleDataPermissionMapper;
import com.recharge.domain.query.MerchantTableQueryDO;
import com.recharge.domain.vo.MerchantTableDO;
import com.recharge.service.MerchantTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors;

@Service
public class MerchantTableServiceImpl extends ServiceImpl<MerchantTableMapper, MerchantTableEntity> implements MerchantTableService {

    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Autowired
    private RoleDataPermissionMapper roleDataPermissionMapper;
    @Autowired
    private MerchantTableMapper merchantTableMapper;

    public ResultDO findTable(MerchantTableQueryDO queryDO) {
        ResultDO result = new ResultSupport();
        List<Map<String,Object>> map = null;
        try {
            map = roleDataPermissionMapper.getAllTable();
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.info("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        QueryWrapper<MerchantTableEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MerchantTableEntity::getTypeId,queryDO.getTypeId());
        List<MerchantTableEntity> entityList = null;
        try {
            entityList=baseMapper.selectList(wrapper);
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.info("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        List<MerchantTableDO> list = null;
        List<MerchantTableEntity> finalEntityList = entityList;
        list = map.stream().map(m->{
            MerchantTableDO merchantTableDO = MapTool.map2Bean(m,MerchantTableDO.class,true);
            if(finalEntityList !=null&& finalEntityList.size()>0){
                for(MerchantTableEntity entity : finalEntityList){
                    if(entity.getTableName().equals(merchantTableDO.getTableName())){
                        merchantTableDO.setCheck(true);
                        break;
                    }
                }
            }
            return merchantTableDO;
        }).collect(Collectors.toList());

        PageDO<MerchantTableDO> pageDO = new PageDO<>();
        pageDO.setData(list);
        result.setModel(ResultDO.FIRST_MODEL_KEY, pageDO);

        return result;
    }

    public ResultDO getTableByType(MerchantTableQueryDO queryDO) {
        ResultDO result = new ResultSupport();

        QueryWrapper<MerchantTableEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MerchantTableEntity::getTypeId,queryDO.getTypeId());
        List<MerchantTableEntity> entityList = null;
        try {
            entityList=baseMapper.selectList(wrapper);
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.info("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        PageDO<MerchantTableEntity> pageDO = new PageDO<>();
        pageDO.setData(entityList);
        result.setModel(ResultDO.FIRST_MODEL_KEY, pageDO);
        return result;
    }

    public ResultDO edit(MerchantTableDO domin){
        ResultDO result = new ResultSupport();

        QueryWrapper<MerchantTableEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(MerchantTableEntity::getTypeId,domin.getTypeIds());

        try {
            baseMapper.delete(wrapper);
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.info("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }
        List<MerchantTableEntity> entityList = null;
        for(String typeId : domin.getTypeIds()){
            for(Map<String,String> tables : domin.getTables()){
                if(entityList==null){
                    entityList=new ArrayList<>();
                }
                MerchantTableEntity entity = new MerchantTableEntity();
                entity.setTypeId(typeId);
                entity.setTableName(tables.get("tableName"));
                entity.setTableComment(tables.get("tableComment"));
                entityList.add(entity);
            }
        }

        try{
            this.saveBatch(entityList);
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.info("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }
        return result;
    }

}
